<?php
/**
 * ShopEx licence
 *
 * -------
 * -------
 */

function widget_goods(&$setting,&$render){
	// add for member price
	if ( isset( $_COOKIE['MLV'] ) ) {
	    $member_level = $_COOKIE['MLV'];
	    (array)$arr = app::get( 'b2c' )->model( 'member_lv' )->getList( 'dis_count', array( 'member_lv_id' => $member_level ) );
	    $dis_count = $arr[0]['dis_count'];

	}
	$objLvprice = app::get( 'b2c' )->model( 'goods_lv_price' );
    (array)$defdis_count = app::get( 'b2c' )->model( 'member_lv' )->getList( 'dis_count', array( 'member_lv_id' => 1 ) );
    $defdis_count = $defdis_count[0]['dis_count'];
    $limit = (intval($setting['limit'])>0)?intval($setting['limit']):6;
    $goods=&app::get('b2c')->model('goods');
    $objProduct = &app::get('b2c')->model('products');
    $config=app::get('b2c')->getConf('site.save_price');
    $data['onSelect']=$setting['onSelect']?$setting['onSelect']:0;
    $setting['max_length']=$setting['max_length']?$setting['max_length']:35;
    $setting['view'] = app::get('b2c')->getConf('gallery.default_view');
    $imageDefault = app::get('image')->getConf('image.set');
    $search = &app::get('b2c')->model('search');
    $setting['str'] = $search->encode($filter);
    $setting['restrict']=$setting['restrict']?$setting['restrict']:'on';
    $order=$setting['goods_orderby']?orderBy($setting['goods_orderby']):null;

    if($setting['columNum']>1){
        for($i=0;$i<$setting['columNum'];$i++){
            parse_str($setting['adjunct']['items'][$i],$filter[$i]);
            $filter[$i] = getFilter($filter[$i]);

            $result['link'][($i-1)]= &kernel::router()->gen_url(
                array(
                    'app'=>'b2c',
                    'ctl'=>'site_gallery',
                    'act'=>'index',
                    'args'=>array($str_cat_id,$search->encode($filter[$i]),($setting['goods_orderby']?$setting['goods_orderby']:0))
                )
            );
            $resultGoods = $goods->getList('*',$filter[$i],0,$limit,$order['sql']);
            if(is_array($resultGoods)){
	            foreach($resultGoods as $pk=>$pv){
	                if(empty($resultGoods[$pk]['mktprice']) ||$resultGoods[$pk]['mktprice'] == '0'){
	                    $resultGoods[$pk]['mktprice'] = $objProduct->getRealMkt($pv['price']);
	                }
	               // add for member price
	               $lv_price = $objLvprice->getList('price',array('goods_id'=>$pv['goods_id'],'level_id'=>$member_level));
               
                   if ( isset( $dis_count ) ) {
                        if(count($lv_price) > 0){    
	                    	$lv_price = end($lv_price);
	                        $resultGoods[$pk]['memprice'] = $lv_price['price'];
	                    }else{
	                        $resultGoods[$pk]['memprice'] = $pv['price'] * $dis_count;
	                    }
                   }else{
                        $resultGoods[$pk]['memprice'] = false;
                   }


	            }
	        }


            $result['goods'][] = $resultGoods;
            $result['defaultImage'] = $imageDefault['S']['default_image'];
            unset($filter[$i]);
        }

        return $result;

    }else{

        parse_str($setting['adjunct']['items'][0],$filter);
        $filter = getFilter($filter);

        $result['link']= &kernel::router()->gen_url(
            array(
                'app' => 'b2c',
                'ctl'=>'site_gallery',
                'act'=>'index',
                'args' => array(implode(",",(array)$filter['cat_id']),$search->encode($filter),($setting['goods_orderby']?$setting['goods_orderby']:0))
            )
        );

        $result['goods']=$goods->getList('*',$filter,0,$limit,$order['sql']);

        if(is_array($result['goods'])){
            foreach($result['goods'] as $pk=>$pv){
                if(empty($result['goods'][$pk]['mktprice']) ||$result['goods'][$pk]['mktprice'] == '0'){
                    $result['goods'][$pk]['mktprice'] = $objProduct->getRealMkt($pv['price']);
                }

                // add for member price
                $lv_price = $objLvprice->getList('price',array('goods_id'=>$pv['goods_id'],'level_id'=>$member_level));
                if ( isset( $dis_count ) ) {
                    if(count($lv_price) > 0){
                    	$lv_price = end($lv_price);
                        $result['goods'][$pk]['memprice'] = $lv_price['price'];
                    }else{
                        $result['goods'][$pk]['memprice'] = $pv['price'] * $dis_count;
                    }
                }else{
                    $result['goods'][$pk]['memprice'] = false;

                }
            }
        }

        $result['defaultImage'] = $imageDefault['S']['default_image'];
        $result['res_url'] = app::get('b2c')->res_url;
        return $result;
    }
}

function getFilter($filter){
    $filter = array_merge(array('marketable'=>"true",'disabled'=>"false",'goods_type'=>"normal"),$filter);
    if($GLOBALS['runtime']['member_lv']){
        $filter['mlevel'] = $GLOBALS['runtime']['member_lv'];
    }
    if($filter['props']){
        foreach($filter['props'] as $k=>$v){
            $filter['p_'.$k]=$v[0];
        }
    }
    return $filter;
}
function orderBy($id=null){

    $o = &app::get('b2c')->model('goods');
    return $o->orderBy( $id );

}
?>
